home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Frameworks / Dragonsmith 1.1.1 / Base files / Utilities / StringUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-15  |  3.0 KB  |  126 lines  |  [TEXT/KAHL]

  1. /*
  2.     StringUtils.c
  3.     
  4.     Created    24 May 1992    
  5.     Modified    25 May 1992    Fixed idiocies in AppendPStr (sheesh!)
  6.             09 Aug 1992    Added CopyPStrFromHandle
  7.             16 Aug 1992    Added PStrToULong
  8.             20 Aug 1992    Fixed CopyPStrFromHandle so it works with 'STR#' (and not 'STR ') resources
  9.             12 Sep 1992    Removed CopyPStrFromHandle — Dragonsmith doesn't use it
  10.             23 Feb 1993    Added GetNumberedPascalString
  11.             18 May 1993    Added ComparePStrings
  12.  
  13.     Copyright © 1992, 1993 by Paul M. Hoffman
  14.     Send comments or suggestions to paul.hoffman@umich.edu -or- dragonsmith@umich.edu
  15.     
  16.     This source code may be freely used, altered, and distributed in any way as long as:
  17.         1.    It is GIVEN away rather than sold (except as expressly permitted by the author)
  18.         2.    This statement and the above copyright notice are left intact.
  19.  
  20. */
  21.  
  22. #include    "StringUtils.h"
  23.  
  24. StringPtr GetNumberedPascalString (ResType resType, short resID, short stringIndex)
  25. {
  26.     StringResHndl        h;
  27.     register short        i, n;
  28.     register StringPtr    p;
  29.     
  30.     if (stringIndex > 0) {
  31.         h = (StringResHndl) GetResource (resType, resID);
  32.         if (h && *h) {
  33.             n = (*h)->numStrings;
  34.             if (stringIndex <= n) {
  35.                 for (p = &((*h)->strings[0]); --stringIndex; )
  36.                     p += p[0] + 1;
  37.                 return p;
  38.             }
  39.         }
  40.     }
  41.     
  42.     return NULL;
  43. }
  44.  
  45. Boolean PStrToULong (Str255 str, long *num)
  46. {
  47.     // Return TRUE if it's a valid number, otherwise FALSE
  48.  
  49.     register unsigned char    i, c, *p;
  50.     unsigned char            len = *str;
  51.     unsigned char            max[] = "\p4294967295";
  52.     
  53.     // Check for too-long numbers
  54.     if (len > 10)
  55.         return FALSE;
  56.         
  57.     // Check for max-length, overly large numbers
  58.     else if (len == 10)
  59.         for (i = 1, p = str + 1; i <= 10; i++) {
  60.             if ((c = *p++) > max[i])
  61.                 return FALSE;
  62.             else if (c < max[i])
  63.                 break;
  64.         }
  65.                 
  66.     for (i = len, p = str + 1; i--; )
  67.         if ((c = *p++) < '0' || c > '9')
  68.             return FALSE;
  69.     StringToNum (str, num);
  70.     return TRUE;
  71. }
  72.  
  73. void SmartCopyPStr (register unsigned char *p1, register unsigned char *p2)
  74. {
  75.     if (p1 == NULL)
  76.         return;
  77.     if (p2 == NULL)                                        // If the destination pointer is NULL,
  78.         p2 = p1;                                        //    just copy the source POINTER
  79.     else if (StripAddress (p1) != StripAddress (p2))            // Otherwise, if the two pointers aren't equal,
  80.         CopyPStr (p1, p2);                                //    go ahead and call CopyPStr
  81. }
  82.  
  83. void CopyPStr (register unsigned char *p1, register unsigned char *p2)
  84. {
  85.     register short    len;
  86.     
  87.     len = (short) *p1;
  88.     do
  89.         *p2++ = *p1++;
  90.     while (len--);
  91. }
  92.  
  93. void AppendPStr (unsigned char *p1, unsigned char *p2)
  94. {
  95.     /* Append the string pointed to by p2 to the string pointed to by p1 */
  96.     register unsigned short    loop;
  97.     register unsigned char    *s1, *s2 = p2;
  98.     unsigned short        lenChange;
  99.     
  100.     lenChange = loop = (unsigned short) *s2++;
  101.     s1 = p1 + *p1 + 1;
  102.     for ( ; loop--; )
  103.         *s1++ = *s2++;
  104.     p1 [0] += lenChange;
  105. }
  106.  
  107. short ComparePStrings (StringPtr str1, StringPtr str2)
  108. {
  109.     register short            len1, len2, i, ch;
  110.     short                minLen, lenDiff;
  111.     register unsigned char    *p1, *p2;
  112.     
  113.     len1 = str1[0];
  114.     len2 = str2[0];
  115.     lenDiff = len1 - len2;
  116.     minLen = (lenDiff < 0 ? len1 : len2);
  117.     
  118.     for (i = minLen, p1 = str1, p2 = str2; i-- ; )
  119.         if (ch = *p1++ - *p2++)
  120.             return ch;
  121.     
  122.     return lenDiff;
  123.     
  124. }
  125.  
  126.